
import 'package:factoryside/api/api.dart';
import 'package:factoryside/provider/sp_helper.dart';
import 'package:factoryside/utils/request_util.dart';
import 'package:factoryside/utils/resource_mananger.dart';
import 'package:factoryside/utils/utils.dart';
import 'package:flutter/material.dart';

class FeedbackPage extends StatefulWidget{
  @override
  State<StatefulWidget> createState() {
    return _FeedbackPage();
  }
}
class _FeedbackPage extends State<FeedbackPage>{

  int _position=1;
  TextEditingController _controller=TextEditingController();
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar:  buildAppBar('意见反馈',null,context),
      body: ListView(
        children: <Widget>[
          Container(
            width: double.infinity,
            height: 120,
            color: Color.fromARGB(255,240,240,240),
            child: Row(
              children: <Widget>[
                Expanded(child:
                 item_select_problem(1,"账号问题","ic_acc.png")
              ),
               Expanded(child:
               item_select_problem(2,"支付问题","ic_zhif.png")),
               Expanded(child:
               item_select_problem(3,"其他问题","ic_qita.png")
               ),


              ],
            ),
          ),
          Container(
            padding: EdgeInsets.only(top: 10,bottom: 10,left: 10),
            width: double.infinity,
            child: Text("反馈内容"),
            decoration: BoxDecoration(
              border: Border(top: BorderSide(width: 0.2,color: Colors.grey),bottom: BorderSide(width: 0.2,color: Colors.grey))
            ),
          ),

          /*输入框*/
          Container(
            margin: EdgeInsets.only(top: 10,bottom: 10,left: 10,right: 10),
            padding: EdgeInsets.only(bottom: 5,left: 10,right: 10),
            width: double.infinity,
            decoration: BoxDecoration(
                color: Color.fromARGB(180,240,240,240),
                border: Border.all(width: 0.2,color: Colors.grey),
                borderRadius: BorderRadius.all(Radius.circular(3))
            ),
            child: TextField(
              controller: _controller,
              style: TextStyle(fontSize: 13,color: Colors.blue),
              maxLength: 100,
              maxLines: 4,
              decoration: InputDecoration(
                disabledBorder: InputBorder.none,
                enabledBorder:  InputBorder.none,
                focusedBorder:  InputBorder.none,
              ),
            )
          ),
          Container(
            margin: EdgeInsets.symmetric(horizontal: 10),
            child: ElevatedButton(
                onPressed: () {
                  if(_controller.text==""){
                    showToast( "请输入反馈内容");
                    return ;
                  }
                  SpHelper.getUserName().then((username)=>_AddOpinion(username, _position.toString(), _controller.text));
                },
                child: Container(
                  padding: EdgeInsets.symmetric(vertical: 15),
                    child: Row(
                      mainAxisAlignment: MainAxisAlignment.center,
                      children: [
                        Text('提交'),
                      ],
                    ))),
          ),
        ],

      ),

    );
  }

  Widget item_select_problem(int index,String problem,String photo){
    return _position==index?
    Container(
      alignment: Alignment.center,
      height: 80,
      margin: EdgeInsets.only(left: 18,right: 18),
      decoration: BoxDecoration(
          borderRadius: BorderRadius.all(Radius.circular(5)),
          color: Colors.red,
          border: Border.all(width: 1.5,color: Colors.orange)
      ),
      child: Column(
        mainAxisAlignment: MainAxisAlignment.center,
        children: <Widget>[
          Image.asset(ImageHelper.wrapAssets(photo),width: 40,height: 40),
          Text("${problem}(已选)",
            style: TextStyle(fontSize:10,color: Colors.white,fontWeight: FontWeight.w500),)
        ],
      ),
    ):
       GestureDetector(
         child: Container(
           alignment: Alignment.center,
           height: 80,
           margin: EdgeInsets.only(left: 18,right: 18),
           decoration: BoxDecoration(
             borderRadius: BorderRadius.all(Radius.circular(5)),
             color: Colors.grey,
           ),
           child: Text(problem,style: TextStyle(fontSize:10,color: Colors.white),),
         ),
         onTap: (){
         setState(() {
           _position=index;
         });
         },
       );

  }


  /*
  *  /**
     * 意见反馈
     *
     * @param UserID   用户id
     * @param BackType 1.账号问题 2.支付问题 3.其他问题
     * @param Content  描述
     * @return
     */
    @FormUrlEncoded
    @POST("Account/AddOpinion")
    Observable<BaseResult<Data<String>>> AddOpinion(@Field("UserID") String UserID,
                                                    @Field("BackType") String BackType,
                                                    @Field("Content") String Content);
  * */
  _AddOpinion(String UserID,String BackType,String Content)async{
    var response = await RequestUtil.getInstance()
        .post(Api.addOpinion, data: {
      'BackType': BackType,
      'UserID': UserID,
      'Content': Content,
    });
    if (response.statusCode == 200) {
      print(response);
      var json=response.data;
      if(json['Data']['Item1']){
        Navigator.pop(context);
        showToast( '反馈成功');
      }else{
        showToast( '保存失败');
      }
    }
  }

}